package function;

public class Navigator {

	private ChainMp3 current;

	public Navigator(Mp3File aCurrent) {
		super();
		current = new ChainMp3(aCurrent, null, null);
	}
	
	public Mp3File gotoPrevious() {
		ChainMp3 previous = current.getPrevious();
		if (previous != null) {
			current = previous;
		}
		return current.getMp3File();
	}
	
	public Mp3File gotoNext() {
		ChainMp3 next = current.getNext();
		if (next != null) {
			current = next;
		}
		return current.getMp3File();
	}
	
	public void setLast(Mp3File mp3) {
		ChainMp3 localCurrent = current;
		while (localCurrent.getNext() != null) {
			localCurrent = localCurrent.getNext();
		}
		ChainMp3 newChainMp3 = new ChainMp3(mp3, localCurrent, null);
		localCurrent.setNext(newChainMp3);
	}
	
	public void setCurrent(Mp3File mp3) {
		ChainMp3 newMp3 = new ChainMp3(mp3, current, null);
		current.setNext(newMp3);
		current = newMp3;
	}

	public Mp3File getPrevious() {
		if (current.getPrevious() == null) {
			return null;
		}
		return current.getPrevious().getMp3File();
	}
	
	public Mp3File getNext() {
		if (current.getNext() == null) {
			return null;
		}
		return current.getNext().getMp3File();
	}

	public Mp3File getCurrent() {
		return current.getMp3File();
	}

	public boolean hasNext() {
		return !(current.getNext() == null);
	}

	public void removeCurrent() {
		current = current.getPrevious();
		if (current != null) {
			current.setNext(null);
		}
	}
}
